package com.lingxu.base.system.manage.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingxu.base.system.manage.entity.MenuTree;
import com.lingxu.base.system.manage.entity.SysUserMenu;
import com.lingxu.base.system.manage.entity.SysUserMenuVos;
import com.lingxu.base.system.manage.mapper.SysUserMenuMapper;
import com.lingxu.base.system.manage.service.ISysUserMenuService;
import com.lingxu.base.system.manage.util.TreeNodeUtil;
import com.lingxu.base.system.manage.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class SysUserMenuServiceImpl extends ServiceImpl<SysUserMenuMapper, SysUserMenu> implements ISysUserMenuService {

    @Resource
    private SysUserMenuMapper sysUserMenuMapper;

    @Override
    public void distributeUserMenus(SysUserMenuVos vos) {
        LoginUser userNow = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userid = vos.getUserid();
        List<String> menuids = vos.getMenuids();
        sysUserMenuMapper.removeAllByUserId(userid);
        for(String menuid:menuids){
            SysUserMenu sysUserMenu = new SysUserMenu();
            sysUserMenu.setUserid(userid);
            sysUserMenu.setMenuid(menuid);
            sysUserMenu.setCreatetime(new Date());
            sysUserMenu.setCreateby(userNow.getId());
            sysUserMenuMapper.insert(sysUserMenu);
        }
    }

    @Override
    public List<MenuTree> queryExistMenuTreeByUserid(String userid) {
        List<MenuTree> treeNodeList = new ArrayList<>();
        List<MenuTree> list = sysUserMenuMapper.queryExistMenuTreeByUserid(userid);
        TreeNodeUtil treeUtil=new TreeNodeUtil<>();
        treeNodeList.addAll(list);
        List<MenuTree> result=treeUtil.list2TreeNode(treeNodeList);
        return result;
    }


    @Override
    public List<MenuTree> queryNotExistMenuTreeByUserid(String userid) {
        List<MenuTree> treeNodeList = new ArrayList<>();
        List<MenuTree> list = sysUserMenuMapper.queryNotExistMenuTreeByUserid(userid);
        TreeNodeUtil treeUtil=new TreeNodeUtil<>();
        treeNodeList.addAll(list);
        List<MenuTree> result=treeUtil.list2TreeNode(treeNodeList);
        return result;
    }

    private static List<MenuTree> buildDepartTreeList(List<MenuTree> treeList){
        //获取parentId = 0的根节点
        List<MenuTree> areaTrees = treeList.stream().filter(item -> item.getPid().equals("0")).collect(Collectors.toList());
        //根据parentId进行分组
        Map<String,List<MenuTree>> map = treeList.stream().collect(Collectors.groupingBy(MenuTree::getPid));
        recursionMenuTree(areaTrees,map);
        return areaTrees;
    }

    private static void recursionMenuTree(List<MenuTree> areaTrees,Map<String,List<MenuTree>> map){
        for(MenuTree areaTree : areaTrees){
            List<MenuTree> childList = map.get(areaTree.getId());
            areaTree.setChildVOS(childList);
            if (null != childList && 0 < childList.size()){
                recursionMenuTree(childList,map);
            }else {
                areaTree.setChildVOS(new ArrayList<>());
            }
        }
    }

}
